package org.company.mars.util;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.pool.PooledConnectionFactory;

public class MQUtils {
    
    private ConnectionFactory factory = null;
    
    private static MQUtils instance = null;
    
    private static String defaultURL = "failover://tcp://localhost:61616";
    
    private MQUtils() {
        ActiveMQConnectionFactory connectionFactory =
                new ActiveMQConnectionFactory(defaultURL);
        factory = new PooledConnectionFactory(connectionFactory);
    }
    
    private ConnectionFactory getFactory() {
        return factory;
    }
    
    public static Connection getConnection() throws JMSException {
        if(instance == null) {
            instance = new MQUtils();
        }
        return instance.getFactory().createConnection();
    }
}
